package com.pi.shisan.test;

import com.pi.shisan.test.encoder.MsgEncoder;
import com.pi.shisan.test.receiver.MessageValid;
import com.pi.shisan.test.receiver.ReceiverDecoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author Shisan
 * @version V1.0.0
 * @Package com.test.server
 * @ClassName: Starter
 * @Description:
 * @date 2019年12月17日 10:54
 */
public class Starter {

    public static void main(String[] args) {

        ServerBootstrap bootstrap = new ServerBootstrap();
        NioEventLoopGroup parent = new NioEventLoopGroup();
        NioEventLoopGroup child = new NioEventLoopGroup();
        ChannelFuture bind = bootstrap.group(parent, child)
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer() {
                    @Override
                    protected void initChannel(Channel channel) throws Exception {
                        channel.pipeline()
                                .addLast(new MessageValid())
                                .addLast(new ReceiverDecoder())
                                .addLast(new MsgEncoder());
                    }
                }).bind(9000);
        bind.addListener(future -> {
            if(future.isSuccess()){
                System.out.println("启动成功！");
            }
        });
    }

}

